package cn.itsource.pethome.product.service.impl;

import cn.itsource.pethome.basic.service.impl.BaseServiceImpl;
import cn.itsource.pethome.product.domain.Product;
import cn.itsource.pethome.product.domain.ProductDetail;
import cn.itsource.pethome.product.mapper.ProductDetailMapper;
import cn.itsource.pethome.product.mapper.ProductMapper;
import cn.itsource.pethome.product.service.IProductService;
import cn.itsource.pethome.utils.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class ProductServiceImpl extends BaseServiceImpl<Product> implements IProductService {

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private ProductDetailMapper productDetailMapper;

    /**
     * 新增产品，先添加产品，在添加产品详情
     * @param product 服务
     */
    @Override
    @Transactional
    public void save(Product product) {
        super.save(product);
        //将返回的主键和表ProductDetail关联起来
        ProductDetail productDetail = product.getProductDetail();
        productDetail.setProductId(product.getId());
        productDetailMapper.save(productDetail);
    }

    /**
     *  修改服务表后，修改服务详情表
     * @param product
     */
    @Override
    @Transactional
    public void update(Product product) {
        super.update(product);
        ProductDetail productDetail = product.getProductDetail();
        productDetail.setProductId(product.getId());
        productDetailMapper.update(productDetail);
    }

    /**
     *  批量上架
     * @param ids 需要上架的id
     * @return
     */
    @Override
    public AjaxResult batchOnSale(Long[] ids) {
        try {
            productMapper.batchOnSale(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult("网络异常，请稍后再试");
        }
    }

    /**
     * 批量下架
     * @param ids 需要下架的id
     * @return
     */
    @Override
    public AjaxResult batchOffSale(Long[] ids) {
        try {
            productMapper.batchOffSale(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult("网络异常，请稍后再试");
        }
    }
}
